﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using Microsoft.International;
using System.Globalization;

namespace Number2Letters
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            rbfrFR.IsChecked = true;
            txtMinValue.Text = "0";
            txtMaxValue.Text = "100";
        }

        private String _selectedCulture;

        private NumberItem GetNumberItem(Int32 value)
        {
            String letters = String.Format(new FormatCultureInfo(this._selectedCulture), "{0:L}", value);
            return new NumberItem(value.ToString(), letters);
        }

        private void btManualTest_Click(object sender, RoutedEventArgs e)
        {
            Int32 value = Int32.Parse(txtTestTheNumber.Text, NumberStyles.AllowThousands, CultureInfo.CurrentCulture);
            lstResults.Items.Insert(0, GetNumberItem(value));
        }

        private void btRandomTest_Click(object sender, RoutedEventArgs e)
        {
            lstResults.Items.Clear();

            Int32 min = Int32.Parse(txtMinValue.Text);
            Int32 max = Int32.MaxValue;
            if (!String.IsNullOrEmpty(txtMaxValue.Text))
                max = Int32.Parse(txtMaxValue.Text);

            Int32 nb = Int32.Parse(cboHowMany.Text);

            List<Int32> lst = new List<Int32>();
            Random rand = new Random();
            for (int i = 0; i < nb; i++)
            {
                Int32 value = rand.Next(min, max + 1);
                if (!lst.Contains(value))
                    lst.Add(value);
            }

            lst = lst.OrderBy(i => i).ToList();

            foreach (Int32 nombre in lst)
                lstResults.Items.Add(GetNumberItem(nombre));
        }

        private void RefreshResults()
        {
            foreach (NumberItem ni in lstResults.Items)
            {
                Int32 tmp = Int32.Parse(ni.lblNumber.Content as String, NumberStyles.AllowThousands, CultureInfo.CurrentCulture);
                ni.lblLetters.Content = String.Format(new FormatCultureInfo(this._selectedCulture), "{0:L}", tmp);
            }
        }

        private void rbX_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            this._selectedCulture = rb.Content as String;

            ResourceDictionary rd = new ResourceDictionary();

            rd.Source = new Uri("ImagesDictionary.xaml", UriKind.RelativeOrAbsolute);
            try
            {
                imgFlag.Source = rd[this._selectedCulture] as ImageSource;
            }
            catch { }

            RefreshResults();
        }

        private void txtTestTheNumber_TextChanged(object sender, TextChangedEventArgs e)
        {
            Int32 tmp;
            btManualTest.IsEnabled = Int32.TryParse(txtTestTheNumber.Text, NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out tmp);
        }

        private void txtMXValue_TextChanged(object sender, TextChangedEventArgs e)
        {
            Boolean ok = false;

            try
            {
                Int32 min, max, nb;
                if (Int32.TryParse(txtMinValue.Text, out min) & Int32.TryParse(txtMaxValue.Text, out max))
                    if (min < max && Int32.TryParse(cboHowMany.Text, out nb))
                        ok = (nb <= 1000);
            }
            catch { }

            btRandomTest.IsEnabled = ok;
        }

        #region Context Menu

        private void menuRemove_Click(object sender, RoutedEventArgs e)
        {
            if (lstResults.SelectedItems.Count > 0)
                lstResults.Items.Remove(lstResults.SelectedItems[0]);
        } 

        private void menuRemoveAll_Click(object sender, RoutedEventArgs e)
        {
            lstResults.Items.Clear();
        }

        private void menuCopy_Click(object sender, RoutedEventArgs e)
        {
            String result = "";
            foreach (NumberItem ni in lstResults.SelectedItems)
                result += String.Format("{0} - {1}\n\r\n", ni.lblNumber.Content, ni.lblLetters.Content);

            Clipboard.SetText(result, TextDataFormat.UnicodeText);
        }

        private void menuCopyAll_Click(object sender, RoutedEventArgs e)
        {
            String result = "";
            foreach (NumberItem ni in lstResults.Items)
                result += String.Format("{0} - {1}\n\r\n", ni.lblNumber.Content, ni.lblLetters.Content);

            Clipboard.SetText(result, TextDataFormat.UnicodeText);
        } 

        #endregion

    }
}